﻿
using System;
using System.Collections.Generic;
using System.Linq;

namespace HeerCards
{
    public class HCPool
    {
        internal List<HCCard> CardCollection = new List<HCCard>();
        internal List<HCLevel> LevelCollections = new List<HCLevel>();
        public void AddLevel(HCLevel level)
        {
            LevelCollections.Add(level);
        }
        public void AddCard(HCCard hCCard)
        {
            CardCollection.Add(hCCard);
        }

        public List<HCCard> getCard(int number)
        {
            var returnList = new List<HCCard>();
            //分配所有ratio范围
            var count = 0;
            foreach (var item in LevelCollections)
            {
                item.min = count;
                count += item.Ratio - 1;
                item.max = count;
                count++;
            }
            //得到最大范围
            var maxRatio = LevelCollections[LevelCollections.Count - 1].max;

            //获得绝对ratio
            var sns = Tool.strangeRanmdom(number);
            foreach (var item in sns)
            {
                var nowRatio = maxRatio * item;
                HCLevel thisLevel = null;
                List<IHCCard> cardList = new List<IHCCard>();
                //取出对应范围
                foreach (var tLevel in LevelCollections)
                {
                    if ((int)nowRatio <= tLevel.max && (int)nowRatio >= tLevel.min)
                    {
                        //必定存在
                        thisLevel = tLevel;

                    }
                }
                //获得所有level的卡
                //2020-1-30更改为linq语句
                IEnumerable<HCCard> a = from n in CardCollection
                                        where n.Level.GUID == thisLevel.GUID
                                        select n;
                foreach (var card in a)
                {
                    cardList.Add(card);
                }

                //排布所有卡片
                foreach (HCCard card in cardList.ToArray())
                {
                    if (card.ExtraProbability <= 1)
                    {
                        cardList.Remove(card);
                        cardList.Add(new vHCCard(card));
                    }
                    else
                    {
                        cardList.Remove(card);
                        //加入vHCCard
                        for (int i = 0; i < card.ExtraProbability; i++)
                        {
                            cardList.Add(new vHCCard(card));
                        }
                    }
                }
                //cardList应该全部被替换成了vHCCard
                returnList.Add(new HCCard((vHCCard)cardList[new Random().Next(0, cardList.Count)]));
                //似乎会丢失extraProa'b，不过没关系


            }
            return returnList;


        }
    }
}
